package com.nf.practice.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.nf.practice.common.R;
import com.nf.practice.entity.User;
import com.nf.practice.service.UserService;
import com.nf.practice.commonutil.DateUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@RestController
@RequestMapping("/practice/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getTimeZone")
    public String getTimeZone(String month) {
        Calendar applyTimeCa = Calendar.getInstance();
        applyTimeCa.setTime(new Date());
        applyTimeCa.set(Calendar.DAY_OF_MONTH, 0);
        applyTimeCa.set(Calendar.HOUR_OF_DAY, 0);
        applyTimeCa.set(Calendar.MINUTE, 0);
        applyTimeCa.set(Calendar.SECOND, 0);
        applyTimeCa.set(Calendar.MILLISECOND, 0);

        // 近3、6、12、13、15、18、24月
        Date applyTimeBf1 = applyTimeCa.getTime();
        String applyTimeBf1Str = DateUtil.dateToString(applyTimeBf1, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -2);
        Date applyTimeBf3 = applyTimeCa.getTime();
        String applyTimeBf3Str = DateUtil.dateToString(applyTimeBf3, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -3);
        Date applyTimeBf6 = applyTimeCa.getTime();
        String applyTimeBf6Str = DateUtil.dateToString(applyTimeBf6, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -6);
        Date applyTimeBf12 = applyTimeCa.getTime();
        String applyTimeBf12Str = DateUtil.dateToString(applyTimeBf12, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -1);
        Date applyTimeBf13 = applyTimeCa.getTime();
        String applyTimeBf13Str = DateUtil.dateToString(applyTimeBf13, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -2);
        Date applyTimeBf15 = applyTimeCa.getTime();
        String applyTimeBf15Str = DateUtil.dateToString(applyTimeBf15, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -3);
        Date applyTimeBf18 = applyTimeCa.getTime();
        String applyTimeBf18Str = DateUtil.dateToString(applyTimeBf18, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -6);
        Date applyTimeBf24 = applyTimeCa.getTime();
        String applyTimeBf24Str = DateUtil.dateToString(applyTimeBf24, DateUtil.DATE_FMT_6);

        System.out.println(month);
        if (getMonthsBetweenDates(applyTimeBf3Str, applyTimeBf1Str).contains(month)) {
            return "在近1到3个月这个区间";
        }

        if (getMonthsBetweenDates(applyTimeBf6Str, applyTimeBf1Str).contains(month)) {
            return "在近1到6个月这个区间";
        }

        if (getMonthsBetweenDates(applyTimeBf12Str, applyTimeBf1Str).contains(month)) {
            return "在近1到12个月这个区间";
        }

        if (getMonthsBetweenDates(applyTimeBf13Str, applyTimeBf1Str).contains(month)) {
            return "在近1到13个月这个区间";
        }

        if (getMonthsBetweenDates(applyTimeBf15Str, applyTimeBf1Str).contains(month)) {
            return "在近1到15个月这个区间";
        }

        if (getMonthsBetweenDates(applyTimeBf18Str, applyTimeBf1Str).contains(month)) {
            return "在近1到18个月这个区间";
        }

        if (getMonthsBetweenDates(applyTimeBf24Str, applyTimeBf1Str).contains(month)) {
            return "在近1到24个月这个区间";
        }

        return "不在任何区间内";
    }

    @PostMapping("/getReport")
    public Map<String, Object> getReport() throws IOException, ParseException {
        File file1 = new File("E:/jsonbaowen/bwtongyong");
        File file2= new File("E:/jsonbaowen/bwxdldz");
        String respJson1 = FileUtils.readFileToString(file1);
        String respJson2 = FileUtils.readFileToString(file2);
        JSONObject resp1 = JSONObject.parseObject(respJson1);
        JSONObject resp2 = JSONObject.parseObject(respJson2);
        JSONObject json1 = resp1.getJSONObject("response");
        JSONObject json2 = resp2.getJSONObject("response");
        JSONObject jsonData = new JSONObject();
        jsonData.putAll(json1);
        jsonData.putAll(json2);


//        File file1 = new File("E:/jsonbaowen/bwtest");
//        String respJson1 = FileUtils.readFileToString(file1);
//        JSONObject jsonData = JSONObject.parseObject(respJson1);

//        Date applyDate = DateUtil.parse(jsonData.getString("applyDate"));
        Date applyDate = DateUtil.parse("2022-03-16");

        Calendar applyTimeCa = Calendar.getInstance();
        applyTimeCa.setTime(applyDate);
        applyTimeCa.set(Calendar.DAY_OF_MONTH, 0);
        applyTimeCa.set(Calendar.HOUR_OF_DAY, 0);
        applyTimeCa.set(Calendar.MINUTE, 0);
        applyTimeCa.set(Calendar.SECOND, 0);
        applyTimeCa.set(Calendar.MILLISECOND, 0);

        // 近3、6、12、13、15、18、24月
        Date applyTimeBf1 = applyTimeCa.getTime();
        String applyTimeBf1Str = DateUtil.dateToString(applyTimeBf1, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -2);
        Date applyTimeBf3 = applyTimeCa.getTime();
        String applyTimeBf3Str = DateUtil.dateToString(applyTimeBf3, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -3);
        Date applyTimeBf6 = applyTimeCa.getTime();
        String applyTimeBf6Str = DateUtil.dateToString(applyTimeBf6, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -6);
        Date applyTimeBf12 = applyTimeCa.getTime();
        String applyTimeBf12Str = DateUtil.dateToString(applyTimeBf12, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -1);
        Date applyTimeBf13 = applyTimeCa.getTime();
        String applyTimeBf13Str = DateUtil.dateToString(applyTimeBf13, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -2);
        Date applyTimeBf15 = applyTimeCa.getTime();
        String applyTimeBf15Str = DateUtil.dateToString(applyTimeBf15, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -3);
        Date applyTimeBf18 = applyTimeCa.getTime();
        String applyTimeBf18Str = DateUtil.dateToString(applyTimeBf18, DateUtil.DATE_FMT_6);

        applyTimeCa.add(Calendar.MONTH, -6);
        Date applyTimeBf24 = applyTimeCa.getTime();
        String applyTimeBf24Str = DateUtil.dateToString(applyTimeBf24, DateUtil.DATE_FMT_6);

        Map<String, Object> resMap = new HashMap<>();
        resMap.put("saleid", jsonData.getJSONObject("biz").getJSONObject("companyInfo").getString("creditNo"));
        resMap.put("salename", jsonData.getJSONObject("biz").getJSONObject("companyInfo").getString("corpName"));

        Date currentDate = new Date();
        String reportDateTime = DateUtil.dateFormatToString(currentDate, "yyyy-MM-dd HH:mm:ss");
        resMap.put("reportDateTime", reportDateTime);
        resMap.put("begintime", String.valueOf(currentDate.getTime() / 1000));


        // 最近24个自然月内每月正常发票金额 BillTotal24m
        List<Map<String, Object>> billTotal24m_list = new ArrayList<>();

        // 最近24个自然月内每月作废发票金额 BillNormalTovoidTotal24m
        List<Map<String, Object>> billNormalTovoidTotal24m_list = new ArrayList<>();

        // 最近24个自然月内每月红冲发票金额 BillHongChong24m
        List<Map<String, Object>> billHongChong24m_list = new ArrayList<>();

        // 最近24个自然月内每月有效开票金额 BillVaild24m
        List<Map<String, Object>> billVaild24m_list = new ArrayList<>();

        // 最近1年内有效开票金额排名前十的购方税号和开票金额 Buyid1Year
        List<Map<String, Object>> buyid1Year_list = new ArrayList<>();

        // 最近2年内有效开票金额排名前十的购方税号和开票金额 Buyid2Year
        List<Map<String, Object>> buyid2Year_list = new ArrayList<>();

        // 最近连续未开票天数 unbilledday
        int unbilledday = -1;

        // 最早一张发票距离现在的月份数 invoicemonth
        int invoicemonth = 0;

        // 最近3个月有效销项发票无税金额 Total3m
        int total3m = 0;

        // 最近3个月有效销项发票无税金额同比增长率 TotalRate3m
        Integer totalRate3m = null;
        int total13_15m = 0;

        // 最近3个月有效销项发票含税总额 JSTotal3m
        double jsTotal3m = 0;

        // 最近3个月有效销项发票含税总额同比增长率 JSTotalRate3m
        Double jsTotalRate3m = null;
        double jsTotalRate13_15m = 0;

        // 最近6个月有效销项发票无税金额 Total6m
        int total6m = 0;

        // 最近6个月有效销项发票无税金额同比增长率 TotalRate6m
        Integer totalRate6m = null;
        int total13_18m = 0;

        // 最近6个月有效销项发票含税总额 JSTotal6m
        double jsTotal6m = 0;

        // 最近6个月有效销项发票含税总额同比增长率 JSTotalRate6m
        Double jsTotalRate6m = null;
        double jsTotalRate13_18m = 0;

        // 最近12个月有效销项发票含税总额标准差 JSBZC12m
        double jsBZC12m = 0;

        // 最近12个月有效销项发票含税总额 JSTotal12m
        double jsTotal12m = 0;

        // 最近12个月有效销项发票含税总额同比增长率 JSTotalRate12
        Double jsTotalRate12m = null;
        double jsTotalRate13_24m = 0;

        // 最近12个月有效销项发票张数 SaleNum12m
        int saleNum12m = 0;

        // 最近12个月是否连续开票 LianXuKaiPiao12m
        int lianXuKaiPiao12mC1_C12 = 1;

        // 最近12个月开票天数 KaiPiaoDay12m
        int kaiPiaoDay12m = 0;

        // 最近12个月未开票月份数 KaiPiaoMonthNm12m
        int kaiPiaoMonthNm12m_count = 0;

        // 最近12个月销项红废票总额 HongFeiTotal12m
        double hongFeiTotal12mH1_H12 = 0;
        double hongFeiTotal12mF1_F12 = 0;

        // 最近12个月下游客户数量 buyidnm12m
        double buyidnm12m_count = 0;

        // 最近12个月前5大客户交易金额 Big5buyid12m (与Buyid1Year逻辑一样)

        // 最近12个月有效销项发票含税总额变异系数 BYXS12m
        double byxs12m = 0;

        // 最近6个月最大连续未开票月份数 NoKaiPiaoJG6m
        List<String> noKaiPiaoJG6m_list = new ArrayList<>();
        int noKaiPiaoJG6m = 0;

        // 最近6个月有效销项发票无税金额环比增长率 TotalQOQ6m
        int totalQOQ6m = 0;
        // 最近6个月所有废票张数占比 TovoidAvgNm6m
        int tovoidAvgNm6m = 0;
        // 最近6个月正常发票废票张数占比 HongChongTovoidAvgNm6m
        int hongChongTovoidAvgNm6m = 0;
        // 最近12个月红票无税金额占比 HongPiaoPer12m
        int hongPiaoPer12m = 0;

        // 最近6个月TOP5客户交易次数占比 Big5buyidNm6m
        List<Map<String, Object>> big5buyidNm6m_list = new ArrayList<>();

        // 最近3个月与最近6个月TOP5客户重合率 Big5BuyOverPer3_6m
        int big5BuyOverPer3_6m = 0;

        // 最近3个月与最近9个月TOP5客户重合率 Big5BuyOverPer3_9m
        int big5BuyOverPer3_9m = 0;

        // 最近3个月与最近12个月TOP5客户重合率 Big5BuyOverPer3_12m
        List<Map<String, Object>> big5BuyOverPer3m_list = new ArrayList<>();
        List<Map<String, Object>> big5BuyOverPer12m_list = new ArrayList<>();

        // 最近3个月与最近24个月TOP5客户重合率 Big5BuyOverPer3_24m
        List<Map<String, Object>> big5BuyOverPerl13l24m_list = new ArrayList<>();
        List<Map<String, Object>> big5BuyOverPer24m_list = new ArrayList<>();

        // 最近3个月与最近6个月TOP10客户重合率 Big10BuyOverPer3_6m
        int big10BuyOverPer3_6m = 0;

        // 最近3个月与最近9个月TOP10客户重合率 Big10BuyOverPer3_9m
        int big10BuyOverPer3_9m = 0;

        // 最近3个月与最近12个月TOP10客户重合率 Big10BuyOverPer3_12m

        // 最近3个月与最近24个月TOP10客户重合率 Big10BuyOverPer3_24m

        // 最近24个月每月销项有效专票开票张数金额应税销售收入开票税额合计 ZuanPiaoSum24m
        List<Map<String, Object>> zuanPiaoSum24m_list = new ArrayList<>();

        // 最近24个月每月销项废票专票开票张数金额应税销售收入开票税额合计 ZuanPiaoTovoidSum24m
        List<Map<String, Object>> zuanPiaoTovoidSum24m_list = new ArrayList<>();

        // 最近24个月每月销项红票专票开票张数金额应税销售收入开票税额合计 ZuanPiaoHongChongSum24m
        List<Map<String, Object>> zuanPiaoHongChongSum24m_list = new ArrayList<>();

        // 最近24个月每月销项有效普票发票开票张数金额应税销售收入开票税额合计 PuPiaoSum24m
        List<Map<String, Object>> puPiaoSum24m_list = new ArrayList<>();

        // 最近24个月每月销项废票普票开票张数金额应税销售收入开票税额合计 PuPiaoTovoidSum24m
        List<Map<String, Object>> puPiaoTovoidSum24m_list = new ArrayList<>();

        // 最近24个月每月销项红票普票开票张数金额应税销售收入开票税额合计 PuPiaoHongChongSum24m
        List<Map<String, Object>> puPiaoHongChongSum24m_list = new ArrayList<>();

        // 最近24个月每月销项有效其他票发票开票张数金额应税销售收入开票税额合计 OtherPiaoSum24m
        List<Map<String, Object>> otherPiaoSum24m_list = new ArrayList<>();

        // 最近24个月每月销项废票其他票开票张数金额应税销售收入开票税额合计 OtherPiaoTovoidSum24m
        List<Map<String, Object>> otherPiaoTovoidSum24m_list = new ArrayList<>();

        // 最近24个月每月销项红票其他票开票张数金额应税销售收入开票税额合计 OtherPiaoHongChongSum24m
        List<Map<String, Object>> otherPiaoHongChongSum24m_list = new ArrayList<>();

        // 最近24个月每月进项有效专票开票张数金额应税销售收入开票税额合计 IncomZuanPiaoSum24m
        List<Map<String, Object>> incomZuanPiaoSum24m_list = new ArrayList<>();

        // 最近24个月每月进项废票专票开票张数金额应税销售收入开票税额合计 IncomZuanPiaoTovoidSum24m
        List<Map<String, Object>> incomZuanPiaoTovoidSum24m_list = new ArrayList<>();

        // 最近24个月每月进项红票专票开票张数金额应税销售收入开票税额合计 IncomZuanPiaoHongChongSum24m
        List<Map<String, Object>> incomZuanPiaoHongChongSum24m_list = new ArrayList<>();

        // 最近24个月每月进项有效普票开票张数金额应税销售收入开票税额合计 IncomPuPiaoSum24m
        List<Map<String, Object>> incomPuPiaoSum24m_list = new ArrayList<>();

        // 最近24个月每月进项废票普票开票张数金额应税销售收入开票税额合计 IncomPuPiaoTovoidSum24m
        List<Map<String, Object>> incomPuPiaoTovoidSum24m_list = new ArrayList<>();

        // 最近24个月每月进项红票普票开票张数金额应税销售收入开票税额合计 IncomPuPiaoHongChongSum24m
        List<Map<String, Object>> incomPuPiaoHongChongSum24m_list = new ArrayList<>();

        // 最近24个月每月进项有效其他票发票开票张数金额应税销售收入开票税额合计 IncomOtherPiaoSum24m
        List<Map<String, Object>> incomOtherPiaoSum24m_list = new ArrayList<>();

        // 最近24个月每月进项废票其他票开票张数金额应税销售收入开票税额合计 IncomOtherPiaoTovoidSum24m
        List<Map<String, Object>> incomOtherPiaoTovoidSum24m_list = new ArrayList<>();

        // 最近24个月每月进项红票其他票开票张数金额应税销售收入开票税额合计 IncomOtherPiaoHongChongSum24m
        List<Map<String, Object>> incomOtherPiaoHongChongSum24m_list = new ArrayList<>();

        // 最近24个月每月的有效含税销售金额TOP5销售客户列表 Big5BuyidNormal24m
        List<Map<String, Object>> big5BuyidNormall12m_list = new ArrayList<>();
        List<Map<String, Object>> big5BuyidNormall13l24m_list = new ArrayList<>();
        List<Map<String, Object>> big5BuyidNormal24m_list = new ArrayList<>();

        // 最近24个月每月的废票含税销售金额TOP5销售客户列表 Big5BuyidNormalTovoid24m
        List<Map<String, Object>> big5BuyidNormalTovoid24m_list = new ArrayList<>();

        // 最近24个月每月的红票含税销售金额TOP5销售客户列表 Big5BuyidHongChong24m
        List<Map<String, Object>> big5BuyidHongChong24m_list = new ArrayList<>();

        // 最近24个月每月客户所在省份数 ProvinceBuyNm24m
        List<Map<String, Object>> ProvinceBuyNm24m_list = new ArrayList<>();


        JSONArray invoiceMonthArray = jsonData.getJSONArray("invoiceMonth");
        if (CollectionUtils.isNotEmpty(invoiceMonthArray)) {
            for (int i = 0; i < invoiceMonthArray.size(); i++) {
                JSONObject invoiceMonthData = invoiceMonthArray.getJSONObject(i);
                String month = null;
                if (StringUtils.isNotBlank(invoiceMonthData.getString("month"))) {
                    if (invoiceMonthData.getString("month").toCharArray().length == 2) {
                        month = invoiceMonthData.getString("year") + "-" + invoiceMonthData.getString("month");
                    } else {
                        month = invoiceMonthData.getString("year") + "-0" + invoiceMonthData.getString("month");
                    }

                    // 近24个月
                    if (!getMonthsBetweenDates(applyTimeBf24Str, applyTimeBf1Str).contains(month)) {
                        continue;
                    }

                    Map<String, Object> billTotal24m_map = new HashMap<>();
                    double billTotal24mValid = 0;
                    double billTotal24mNegative = 0;
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmountTax"))) {
                        billTotal24mValid = Double.valueOf(invoiceMonthData.getString("validAmountTax"));
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("negativeAmountTax"))) {
                        billTotal24mNegative = Double.valueOf(invoiceMonthData.getString("negativeAmountTax"));
                    }
                    billTotal24m_map.put("total", billTotal24mValid - billTotal24mNegative);
                    billTotal24m_map.put("month", month);
                    billTotal24m_list.add(billTotal24m_map);

                    Map<String, Object> billNormalTovoidTotal24m_map = new HashMap<>();
                    double billNormalTovoidTotal24mTotal = 0;
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("invalidAmountTax"))) {
                        billNormalTovoidTotal24mTotal = Double.valueOf(invoiceMonthData.getString("invalidAmountTax"));
                    }
                    billNormalTovoidTotal24m_map.put("total", billNormalTovoidTotal24mTotal);
                    billNormalTovoidTotal24m_map.put("month", month);
                    billNormalTovoidTotal24m_list.add(billNormalTovoidTotal24m_map);

                    Map<String, Object> billHongChong24m_map = new HashMap<>();
                    double billHongChong24mTotal = 0;
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("negativeAmountTax"))) {
                        billHongChong24mTotal = Double.valueOf(invoiceMonthData.getString("negativeAmountTax"));
                    }
                    billHongChong24m_map.put("total", billHongChong24mTotal);
                    billHongChong24m_map.put("month", month);
                    billHongChong24m_list.add(billHongChong24m_map);

                    Map<String, Object> billVaild24m_map = new HashMap<>();
                    double billVaild24mTotal = 0;
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmountTax"))) {
                        billVaild24mTotal = Double.valueOf(invoiceMonthData.getString("validAmountTax"));
                    }
                    billVaild24m_map.put("total", billVaild24mTotal);
                    billVaild24m_map.put("month", month);
                    billVaild24m_list.add(billVaild24m_map);

                    Map<String, Object> zuanPiaoSum24m_map = new HashMap<>();
                    double zuanPiaoSum24mTotal = 0;
                    double zuanPiaoSum24mJSTotal = 0;
                    int zuanPiaoSum24m_nm = 0;
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmountSpecial"))) {
                        zuanPiaoSum24mTotal = Double.valueOf(invoiceMonthData.getString("validAmountSpecial"));
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmountTaxSpecial"))) {
                        zuanPiaoSum24mJSTotal = Double.valueOf(invoiceMonthData.getString("validAmountTaxSpecial"));
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validCountSpecial"))) {
                        zuanPiaoSum24m_nm = Integer.valueOf(invoiceMonthData.getString("validCountSpecial"));
                    }
                    zuanPiaoSum24m_map.put("total", zuanPiaoSum24mTotal);
                    zuanPiaoSum24m_map.put("jstotal", zuanPiaoSum24mJSTotal);
                    zuanPiaoSum24m_map.put("month", month);
                    zuanPiaoSum24m_map.put("taxtotal", zuanPiaoSum24mJSTotal - zuanPiaoSum24mTotal);
                    zuanPiaoSum24m_map.put("nm", zuanPiaoSum24m_nm);
                    zuanPiaoSum24m_list.add(billVaild24m_map);

                    Map<String, Object> zuanPiaoTovoidSum24m_map = new HashMap<>();
                    double zuanPiaoTovoidSum24mTotal = 0;
                    double zuanPiaoTovoidSum24mJSTotal = 0;
                    int zuanPiaoTovoidSum24m_nm = 0;
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("invalidAmountSpecial"))) {
                        zuanPiaoTovoidSum24mTotal = Double.valueOf(invoiceMonthData.getString("invalidAmountSpecial"));
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("invalidAmountTaxSpecial"))) {
                        zuanPiaoTovoidSum24mJSTotal = Double.valueOf(invoiceMonthData.getString("invalidAmountTaxSpecial"));
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("invalidCountSpecial"))) {
                        zuanPiaoTovoidSum24m_nm = Integer.valueOf(invoiceMonthData.getString("invalidCountSpecial"));
                    }
                    zuanPiaoTovoidSum24m_map.put("total", zuanPiaoTovoidSum24mTotal);
                    zuanPiaoTovoidSum24m_map.put("jstotal", zuanPiaoTovoidSum24mJSTotal);
                    zuanPiaoTovoidSum24m_map.put("month", month);
                    zuanPiaoTovoidSum24m_map.put("taxtotal", zuanPiaoTovoidSum24mJSTotal - zuanPiaoTovoidSum24mTotal);
                    zuanPiaoTovoidSum24m_map.put("nm", zuanPiaoTovoidSum24m_nm);
                    zuanPiaoTovoidSum24m_list.add(zuanPiaoTovoidSum24m_map);

                    Map<String, Object> zuanPiaoHongChongSum24m_map = new HashMap<>();
                    double zuanPiaoHongChongSum24mTotal = 0;
                    double zuanPiaoHongChongSum24mJSTotal = 0;
                    int zuanPiaoHongChongSum24m_nm = 0;
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("negativeAmountSpecial"))) {
                        zuanPiaoHongChongSum24mTotal = Double.valueOf(invoiceMonthData.getString("negativeAmountSpecial"));
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("negativeAmountTaxSpecial"))) {
                        zuanPiaoHongChongSum24mJSTotal = Double.valueOf(invoiceMonthData.getString("negativeAmountTaxSpecial"));
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("negativeCountSpecial"))) {
                        zuanPiaoHongChongSum24m_nm = Integer.valueOf(invoiceMonthData.getString("negativeCountSpecial"));
                    }
                    zuanPiaoHongChongSum24m_map.put("total", zuanPiaoHongChongSum24mTotal);
                    zuanPiaoHongChongSum24m_map.put("jstotal", zuanPiaoHongChongSum24mJSTotal);
                    zuanPiaoHongChongSum24m_map.put("month", month);
                    zuanPiaoHongChongSum24m_map.put("taxtotal", zuanPiaoHongChongSum24mJSTotal - zuanPiaoHongChongSum24mTotal);
                    zuanPiaoHongChongSum24m_map.put("nm", zuanPiaoHongChongSum24m_nm);
                    zuanPiaoHongChongSum24m_list.add(zuanPiaoHongChongSum24m_map);

                    Map<String, Object> puPiaoSum24m_map = new HashMap<>();
                    double puPiaoSum24mTotal = 0;
                    double puPiaoSum24mJSTotal = 0;
                    int puPiaoSum24m_nm = 0;
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmountCommon"))) {
                        puPiaoSum24mTotal = Double.valueOf(invoiceMonthData.getString("validAmountCommon"));
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmountTaxCommon"))) {
                        puPiaoSum24mJSTotal = Double.valueOf(invoiceMonthData.getString("validAmountTaxCommon"));
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validCountCommon"))) {
                        puPiaoSum24m_nm = Integer.valueOf(invoiceMonthData.getString("validCountCommon"));
                    }
                    puPiaoSum24m_map.put("total", puPiaoSum24mTotal);
                    puPiaoSum24m_map.put("jstotal", puPiaoSum24mJSTotal);
                    puPiaoSum24m_map.put("month", month);
                    puPiaoSum24m_map.put("taxtotal", puPiaoSum24mJSTotal - puPiaoSum24mTotal);
                    puPiaoSum24m_map.put("nm", puPiaoSum24m_nm);
                    puPiaoSum24m_list.add(puPiaoSum24m_map);

                    Map<String, Object> puPiaoTovoidSum24m_map = new HashMap<>();
                    double puPiaoTovoidSum24mTotal = 0;
                    double puPiaoTovoidSum24mJSTotal = 0;
                    int puPiaoTovoidSum24m_nm = 0;
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("invalidAmountCommon"))) {
                        puPiaoTovoidSum24mTotal = Double.valueOf(invoiceMonthData.getString("invalidAmountCommon"));
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("invalidAmountTaxCommon"))) {
                        puPiaoTovoidSum24mJSTotal = Double.valueOf(invoiceMonthData.getString("invalidAmountTaxCommon"));
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("invalidCountCommon"))) {
                        puPiaoTovoidSum24m_nm = Integer.valueOf(invoiceMonthData.getString("invalidCountCommon"));
                    }
                    puPiaoTovoidSum24m_map.put("total", puPiaoTovoidSum24mTotal);
                    puPiaoTovoidSum24m_map.put("jstotal", puPiaoTovoidSum24mJSTotal);
                    puPiaoTovoidSum24m_map.put("month", month);
                    puPiaoTovoidSum24m_map.put("taxtotal", puPiaoTovoidSum24mJSTotal - puPiaoTovoidSum24mTotal);
                    puPiaoTovoidSum24m_map.put("nm", puPiaoTovoidSum24m_nm);
                    puPiaoTovoidSum24m_list.add(puPiaoTovoidSum24m_map);

                    Map<String, Object> puPiaoHongChongSum24m_map = new HashMap<>();
                    double puPiaoHongChongSum24mTotal = 0;
                    double puPiaoHongChongSum24mJSTotal = 0;
                    int puPiaoHongChongSum24m_nm = 0;
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("negativeAmountCommon"))) {
                        puPiaoHongChongSum24mTotal = Double.valueOf(invoiceMonthData.getString("negativeAmountCommon"));
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("negativeAmountTaxCommon"))) {
                        puPiaoHongChongSum24mJSTotal = Double.valueOf(invoiceMonthData.getString("negativeAmountTaxCommon"));
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("negativeCountCommon"))) {
                        puPiaoHongChongSum24m_nm = Integer.valueOf(invoiceMonthData.getString("negativeCountCommon"));
                    }
                    puPiaoHongChongSum24m_map.put("total", puPiaoHongChongSum24mTotal);
                    puPiaoHongChongSum24m_map.put("jstotal", puPiaoHongChongSum24mJSTotal);
                    puPiaoHongChongSum24m_map.put("month", month);
                    puPiaoHongChongSum24m_map.put("taxtotal", puPiaoTovoidSum24mJSTotal - puPiaoTovoidSum24mTotal);
                    puPiaoHongChongSum24m_map.put("nm", puPiaoHongChongSum24m_nm);
                    puPiaoHongChongSum24m_list.add(puPiaoHongChongSum24m_map);

                    Map<String, Object> otherPiaoSum24m_map = new HashMap<>();
                    double otherPiaoSum24mTotal = 0;
                    double otherPiaoSum24mJSTotal = 0;
                    int otherPiaoSum24m_nm = 0;
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmount"))) {
                        otherPiaoSum24mTotal =
                                Double.valueOf(invoiceMonthData.getString("validAmount")) - zuanPiaoSum24mTotal - puPiaoSum24mTotal;
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmountTax"))) {
                        otherPiaoSum24mJSTotal =
                                Double.valueOf(invoiceMonthData.getString("validAmountTax")) - zuanPiaoSum24mJSTotal - puPiaoSum24mJSTotal;
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validCount"))) {
                        otherPiaoSum24m_nm =
                                Integer.valueOf(invoiceMonthData.getString("validCount")) - zuanPiaoSum24m_nm - puPiaoSum24m_nm;
                    }
                    otherPiaoSum24m_map.put("total", otherPiaoSum24mTotal);
                    otherPiaoSum24m_map.put("jstotal", otherPiaoSum24mJSTotal);
                    otherPiaoSum24m_map.put("month", month);
                    otherPiaoSum24m_map.put("taxtotal", otherPiaoSum24mJSTotal - otherPiaoSum24mTotal);
                    otherPiaoSum24m_map.put("nm", otherPiaoSum24m_nm);
                    otherPiaoSum24m_list.add(puPiaoHongChongSum24m_map);

                    Map<String, Object> otherPiaoTovoidSum24m_map = new HashMap<>();
                    double otherPiaoTovoidSum24mTotal = 0;
                    double otherPiaoTovoidSum24mJSTotal = 0;
                    int otherPiaoTovoidSum24m_nm = 0;
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("invalidAmount"))) {
                        otherPiaoTovoidSum24mTotal =
                                Double.valueOf(invoiceMonthData.getString("invalidAmount")) - zuanPiaoTovoidSum24mTotal - puPiaoTovoidSum24mTotal;
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("invalidAmountTax"))) {
                        otherPiaoTovoidSum24mJSTotal =
                                Double.valueOf(invoiceMonthData.getString("invalidAmountTax")) - zuanPiaoTovoidSum24mJSTotal - puPiaoTovoidSum24mJSTotal;
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("invalidCount"))) {
                        otherPiaoTovoidSum24m_nm =
                                Integer.valueOf(invoiceMonthData.getString("invalidCount")) - zuanPiaoTovoidSum24m_nm - puPiaoTovoidSum24m_nm;
                    }
                    otherPiaoTovoidSum24m_map.put("total", otherPiaoTovoidSum24mTotal);
                    otherPiaoTovoidSum24m_map.put("jstotal", otherPiaoTovoidSum24mJSTotal);
                    otherPiaoTovoidSum24m_map.put("month", month);
                    otherPiaoTovoidSum24m_map.put("taxtotal", otherPiaoTovoidSum24mJSTotal - otherPiaoTovoidSum24mTotal);
                    otherPiaoTovoidSum24m_map.put("nm", otherPiaoTovoidSum24m_nm);
                    otherPiaoTovoidSum24m_list.add(otherPiaoTovoidSum24m_map);

                    Map<String, Object> otherPiaoHongChongSum24m_map = new HashMap<>();
                    double otherPiaoHongChongSum24mTotal = 0;
                    double otherPiaoHongChongSum24mJSTotal = 0;
                    int otherPiaoHongChongSum24m_nm = 0;
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("negativeAmount"))) {
                        otherPiaoHongChongSum24mTotal =
                                Double.valueOf(invoiceMonthData.getString("negativeAmount")) - zuanPiaoHongChongSum24mTotal - puPiaoHongChongSum24mTotal;
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("negativeAmountTax"))) {
                        otherPiaoHongChongSum24mJSTotal =
                                Double.valueOf(invoiceMonthData.getString("negativeAmountTax")) - zuanPiaoHongChongSum24mJSTotal - puPiaoHongChongSum24mJSTotal;
                    }
                    if (StringUtils.isNotBlank(invoiceMonthData.getString("negativeCount"))) {
                        otherPiaoHongChongSum24m_nm =
                                Integer.valueOf(invoiceMonthData.getString("negativeCount")) - zuanPiaoHongChongSum24m_nm - puPiaoHongChongSum24m_nm;
                    }
                    otherPiaoHongChongSum24m_map.put("total", otherPiaoHongChongSum24mTotal);
                    otherPiaoHongChongSum24m_map.put("jstotal", otherPiaoHongChongSum24mJSTotal);
                    otherPiaoHongChongSum24m_map.put("month", month);
                    otherPiaoHongChongSum24m_map.put("taxtotal", otherPiaoHongChongSum24mJSTotal - otherPiaoHongChongSum24mTotal);
                    otherPiaoHongChongSum24m_map.put("nm", otherPiaoHongChongSum24m_nm);
                    otherPiaoHongChongSum24m_list.add(otherPiaoHongChongSum24m_map);


                    // 近13到24个月
                    if (getMonthsBetweenDates(applyTimeBf24Str, applyTimeBf13Str).contains(month)) {
                        if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmountTax"))) {
                            jsTotalRate13_24m += Double.valueOf(invoiceMonthData.getString("validAmountTax"));
                        }
                    }

                    // 近13到18个月
                    if (getMonthsBetweenDates(applyTimeBf18Str, applyTimeBf13Str).contains(month)) {
                        if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmount"))) {
                            total13_18m += Double.valueOf(invoiceMonthData.getString("validAmount"));
                        }

                        if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmountTax"))) {
                            jsTotalRate13_18m += Double.valueOf(invoiceMonthData.getString("validAmountTax"));
                        }
                    }

                    // 近13到15个月
                    if (getMonthsBetweenDates(applyTimeBf15Str, applyTimeBf13Str).contains(month)) {
                        if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmount"))) {
                            total13_15m += Double.valueOf(invoiceMonthData.getString("validAmount"));
                        }

                        if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmountTax"))) {
                            jsTotalRate13_15m += Double.valueOf(invoiceMonthData.getString("validAmountTax"));
                        }
                    }

                    // 近1到12个月
                    if (!getMonthsBetweenDates(applyTimeBf12Str, applyTimeBf1Str).contains(month)) {
                        continue;
                    }

                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmountTax"))) {
                        jsTotal12m += Double.valueOf(invoiceMonthData.getString("validAmountTax"));
                    }

                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validCount"))) {
                        saleNum12m += Integer.valueOf(invoiceMonthData.getString("validCount"));
                    }

                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validCount"))) {
                        if (invoiceMonthData.getString("validCount").equals("0")) {
                            lianXuKaiPiao12mC1_C12 = 0;
                            kaiPiaoMonthNm12m_count++;
                        }
                    }

                    if (StringUtils.isNotBlank(invoiceMonthData.getString("negativeAmountTax"))) {
                        hongFeiTotal12mH1_H12 += Math.abs(Double.valueOf(invoiceMonthData.getString("negativeAmountTax")));
                    }

                    if (StringUtils.isNotBlank(invoiceMonthData.getString("invalidAmountTax"))) {
                        hongFeiTotal12mF1_F12 += Double.valueOf(invoiceMonthData.getString("invalidAmountTax"));
                    }

                    // 近1到6个月
                    if (!getMonthsBetweenDates(applyTimeBf6Str, applyTimeBf1Str).contains(month)) {
                        continue;
                    }

                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmount"))) {
                        total6m += Integer.valueOf(invoiceMonthData.getString("validAmount"));
                    }

                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmountTax"))) {
                        jsTotal6m += Double.valueOf(invoiceMonthData.getString("validAmountTax"));
                    }

                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validCount"))) {
                        if (invoiceMonthData.getString("validCount").equals("0")) {
                            noKaiPiaoJG6m_list = new ArrayList<>();
                            noKaiPiaoJG6m_list.add(month);
                        }
                    }


                    // 近1到3个月
                    if (!getMonthsBetweenDates(applyTimeBf3Str, applyTimeBf1Str).contains(month)) {
                        continue;
                    }

                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmount"))) {
                        total3m += Double.valueOf(invoiceMonthData.getString("validAmount"));
                    }

                    if (StringUtils.isNotBlank(invoiceMonthData.getString("validAmountTax"))) {
                        jsTotal3m += Double.valueOf(invoiceMonthData.getString("validAmountTax"));
                    }

                }
            }

        }

        JSONArray l3Array = jsonData.getJSONObject("topCorpByAmount").getJSONArray("l3");
        if (CollectionUtils.isNotEmpty(l3Array)) {
            for (int i = 0; i < l3Array.size(); i++) {
                JSONObject l3Data = l3Array.getJSONObject(i);
                Map<String, Object> big5BuyOverPer3m_map = new HashMap<>();
                if (StringUtils.isNotBlank(l3Data.getString("amount"))) {
                    double big5BuyOverPer3mTotal = Double.valueOf(l3Data.getString("amount"));
                    big5BuyOverPer3m_map.put("total", big5BuyOverPer3mTotal);
                }
                if (StringUtils.isNotBlank(l3Data.getString("taxNo"))) {
                    big5BuyOverPer3m_map.put("buyid", l3Data.getString("taxNo"));
                }
                if (!big5BuyOverPer3m_map.isEmpty()) {
                    big5BuyOverPer3m_list.add(big5BuyOverPer3m_map);
                }
            }
        }

        JSONArray l12Array = jsonData.getJSONObject("topCorpByAmount").getJSONArray("l12");
        if (CollectionUtils.isNotEmpty(l12Array)) {
            for (int i = 0; i < l12Array.size(); i++) {
                JSONObject l12Data = l12Array.getJSONObject(i);
                Map<String, Object> buyid1Year_map = new HashMap<>();
                if (StringUtils.isNotBlank(l12Data.getString("amount"))) {
                    double buyid1YearTotal = Double.valueOf(l12Data.getString("amount"));
                    buyid1Year_map.put("total", buyid1YearTotal);
                }
                if (StringUtils.isNotBlank(l12Data.getString("taxNo"))) {
                    buyid1Year_map.put("buyid", l12Data.getString("taxNo"));
                }
                if (!buyid1Year_map.isEmpty()) {
                    buyid1Year_list.add(buyid1Year_map);
                }

                if (StringUtils.isNotBlank(l12Data.getString("corpCount"))) {
                    buyidnm12m_count += Double.valueOf(l12Data.getString("corpCount"));
                }

                Map<String, Object> big5BuyOverPer12m_map = new HashMap<>();
                if (StringUtils.isNotBlank(l12Data.getString("amount"))) {
                    double big5BuyOverPer12mTotal = Double.valueOf(l12Data.getString("amount"));
                    big5BuyOverPer12m_map.put("total", big5BuyOverPer12mTotal);
                }
                if (StringUtils.isNotBlank(l12Data.getString("taxNo"))) {
                    big5BuyOverPer12m_map.put("buyid", l12Data.getString("taxNo"));
                }
                if (!big5BuyOverPer12m_map.isEmpty()) {
                    big5BuyOverPer12m_list.add(big5BuyOverPer12m_map);
                }
            }
        }

        JSONArray l13_l24Array = jsonData.getJSONObject("topCorpByAmount").getJSONArray("l13_l24");
        if (CollectionUtils.isNotEmpty(l13_l24Array)) {
            for (int i = 0; i < l13_l24Array.size(); i++) {
                JSONObject l13_l24Data = l13_l24Array.getJSONObject(i);
                Map<String, Object> buyid2Year_map = new HashMap<>();
                if (StringUtils.isNotBlank(l13_l24Data.getString("amount"))) {
                    double buyid2YearTotal = Double.valueOf(l13_l24Data.getString("amount"));
                    buyid2Year_map.put("total", buyid2YearTotal);
                }
                if (StringUtils.isNotBlank(l13_l24Data.getString("taxNo"))) {
                    buyid2Year_map.put("buyid", l13_l24Data.getString("taxNo"));
                }
                if (!buyid2Year_map.isEmpty()) {
                    buyid2Year_list.add(buyid2Year_map);
                }

                Map<String, Object> big5BuyOverPerl13l24m_map = new HashMap<>();
                if (StringUtils.isNotBlank(l13_l24Data.getString("amount"))) {
                    double big5BuyOverPerl13l24mTotal = Double.valueOf(l13_l24Data.getString("amount"));
                    big5BuyOverPerl13l24m_map.put("total", big5BuyOverPerl13l24mTotal);
                }
                if (StringUtils.isNotBlank(l13_l24Data.getString("taxNo"))) {
                    big5BuyOverPerl13l24m_map.put("buyid", l13_l24Data.getString("taxNo"));
                }
                if (!big5BuyOverPerl13l24m_map.isEmpty()) {
                    big5BuyOverPerl13l24m_list.add(big5BuyOverPerl13l24m_map);
                }
            }
        }

        JSONArray l12AreaArray = jsonData.getJSONObject("topAreaByAmount").getJSONArray("l12");
        if (CollectionUtils.isNotEmpty(l12AreaArray)) {
            for (int i = 0; i < l12AreaArray.size(); i++) {
                JSONObject l12AreaData = l12AreaArray.getJSONObject(i);
                Map<String, Object> big5BuyidNormall12m_map = new HashMap<>();
                if (StringUtils.isNotBlank(l12AreaData.getString("amount"))) {
                    double big5BuyidNormal12mTotal = Double.valueOf(l12AreaData.getString("amount"));
                    big5BuyidNormall12m_map.put("total", big5BuyidNormal12mTotal);
                }
                if (StringUtils.isNotBlank(l12AreaData.getString("taxNo"))) {
                    big5BuyidNormall12m_map.put("buyid", l12AreaData.getString("taxNo"));
                }
                if (!big5BuyidNormall12m_map.isEmpty()) {
                    big5BuyidNormall12m_list.add(big5BuyidNormall12m_map);
                }
            }
        }

        JSONArray l13_l24AreaArray = jsonData.getJSONObject("topAreaByAmount").getJSONArray("l13_l24");
        if (CollectionUtils.isNotEmpty(l13_l24AreaArray)) {
            for (int i = 0; i < l13_l24AreaArray.size(); i++) {
                JSONObject l13_l24AreaData = l13_l24AreaArray.getJSONObject(i);
                Map<String, Object> big5BuyidNormall13l24m_map = new HashMap<>();
                if (StringUtils.isNotBlank(l13_l24AreaData.getString("amount"))) {
                    double big5BuyidNormall13l24mTotal = Double.valueOf(l13_l24AreaData.getString("amount"));
                    big5BuyidNormall13l24m_map.put("total", big5BuyidNormall13l24mTotal);
                }
                if (StringUtils.isNotBlank(l13_l24AreaData.getString("taxNo"))) {
                    big5BuyidNormall13l24m_map.put("buyid", l13_l24AreaData.getString("taxNo"));
                }
                if (!big5BuyidNormall13l24m_map.isEmpty()) {
                    big5BuyidNormall13l24m_list.add(big5BuyidNormall13l24m_map);
                }
            }
        }

        resMap.put("BillTotal24m", sortByStr(billTotal24m_list));
        resMap.put("BillNormalTovoidTotal24m", sortByStr(billNormalTovoidTotal24m_list));
        resMap.put("BillHongChong24m", sortByStr(billHongChong24m_list));
        resMap.put("BillVaild24m", sortByStr(billVaild24m_list));

        List<Map<String, Object>> buyid1Year_sortList = sortByDouble(buyid1Year_list);
        if (CollectionUtils.isNotEmpty(buyid1Year_sortList)) {
            resMap.put("Buyid1Year", buyid1Year_sortList.subList(0, buyid1Year_sortList.size() > 10 ? 10 : buyid1Year_sortList.size()));
        }

        List<Map<String, Object>> buyid2Year_sortList = sortByDouble(buyid2Year_list);
        if (CollectionUtils.isNotEmpty(buyid2Year_sortList)) {
            resMap.put("Buyid2Year", buyid2Year_sortList.subList(0, buyid2Year_sortList.size() > 10 ? 10 : buyid2Year_sortList.size()));
        }

        String lastInvoiceDate = jsonData.getString("lastInvoiceDate");
        if (StringUtils.isNotBlank(lastInvoiceDate)) {
            // 最近一条开票时间
            LocalDate recentTime = DateUtil.stringToLocalDate(jsonData.getString("lastInvoiceDate"), DateUtil.DATE_FMT_0);

            // 当前申请时间最近的第一个月
            LocalDate endLastTime = DateUtil.dateToLocalDate(applyTimeBf1);
            unbilledday = DateUtil.getAbsDateDiffDay(endLastTime, recentTime);
        }
        resMap.put("unbilledday", unbilledday);


        String firstInvoiceDate = jsonData.getString("firstInvoiceDate");
        if (StringUtils.isNotBlank(firstInvoiceDate)) {
            // 最早一条记录开票日期
            LocalDate recentTime = DateUtil.stringToLocalDate(jsonData.getString("firstInvoiceDate"), DateUtil.DATE_FMT_0);

            // 当前申请时间最近的第一个月
            LocalDate endLastTime = DateUtil.dateToLocalDate(applyTimeBf1);
            invoicemonth = DateUtil.getAbsDateDiffMonth(recentTime, endLastTime);
        }
        resMap.put("invoicemonth", invoicemonth);

        resMap.put("Total3m", total3m);
        if (total13_15m > 0) {
            totalRate3m = (total3m - total13_15m) / total13_15m;
        }
        resMap.put("TotalRate3m", totalRate3m);

        resMap.put("JSTotal3m", jsTotal3m);
        if (jsTotalRate13_15m > 0) {
            jsTotalRate3m = (jsTotal3m - jsTotalRate13_15m) / jsTotalRate13_15m;
        }
        resMap.put("JSTotalRate3m", jsTotalRate3m);

        resMap.put("Total6m", total6m);
        if (total13_18m > 0) {
            totalRate6m = (total6m - total13_18m) / total13_18m;
        }
        resMap.put("TotalRate6m", totalRate6m);

        resMap.put("JSTotal6m", jsTotal6m);
        if (jsTotalRate13_18m > 0) {
            jsTotalRate6m = (jsTotal6m - jsTotalRate13_18m) / jsTotalRate13_18m;
        }
        resMap.put("JSTotalRate6m", jsTotalRate6m);

        resMap.put("JSTotal12m", jsTotal12m);
        if (jsTotalRate13_24m > 0) {
            jsTotalRate12m = (jsTotal12m - jsTotalRate13_24m) / jsTotalRate13_24m;
        }
        resMap.put("JSTotalRate12m", jsTotalRate12m);

        resMap.put("JSBZC12m", jsBZC12m);

        resMap.put("SaleNum12m", saleNum12m);

        resMap.put("LianXuKaiPiao12m", lianXuKaiPiao12mC1_C12 == 0 ? "0" : "1");

        if (StringUtils.isNotBlank(jsonData.getString("invoiceDayCountL12M"))) {
            kaiPiaoDay12m = Integer.valueOf(jsonData.getString("invoiceDayCountL12M"));
        }
        resMap.put("KaiPiaoDay12m", kaiPiaoDay12m);

        resMap.put("KaiPiaoMonthNm12m", kaiPiaoMonthNm12m_count);

        // 最近12个月最大开票间隔天数 KaiPiaoJGTX12m
        if (StringUtils.isNotBlank(jsonData.getString("invoiceInternaL12M"))) {
            String kaiPiaoJGTX12m = jsonData.getString("invoiceInternaL12M");
            resMap.put("KaiPiaoJGTX12m", kaiPiaoJGTX12m);
        }

        resMap.put("HongFeiTotal12m", hongFeiTotal12mH1_H12 + hongFeiTotal12mF1_F12);

        resMap.put("buyidnm12m", buyidnm12m_count);

        if (CollectionUtils.isNotEmpty(buyid1Year_sortList)) {
            resMap.put("Big5buyid12m", buyid1Year_sortList.subList(0, buyid1Year_sortList.size() > 5 ? 5 : buyid1Year_sortList.size()));
        }

        resMap.put("TotalQOQ6m", totalQOQ6m);
        resMap.put("TovoidAvgNm6m", tovoidAvgNm6m);
        resMap.put("HongChongTovoidAvgNm6m", hongChongTovoidAvgNm6m);
        resMap.put("HongPiaoPer12m", hongPiaoPer12m);

        Map<String, Object> big5buyidNm6m_map = new HashMap<>();
        big5buyidNm6m_map.put("buyid", "");
        big5buyidNm6m_map.put("nm", "");
        big5buyidNm6m_list.add(big5buyidNm6m_map);
        resMap.put("Big5buyidNm6m", big5buyidNm6m_list);

        resMap.put("Big5BuyOverPer3_6m", big5BuyOverPer3_6m);
        resMap.put("Big5BuyOverPer3_9m", big5BuyOverPer3_9m);

        List<Map<String, Object>> big5BuyOverPer3m_sortList = sortByDouble(big5BuyOverPer3m_list);
        List<Map<String, Object>> big5BuyOverPer12m_sortList = sortByDouble(big5BuyOverPer12m_list);
        List<String> big5BuyOverPer3mTop5TaxNoList = new ArrayList<>();
        List<String> big5BuyOverPer12mTop5TaxNoList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(big5BuyOverPer3m_sortList)) {
            List<Map<String, Object>> big5BuyOverPer3m_Top5List = big5BuyOverPer3m_sortList.subList(0, big5BuyOverPer3m_sortList.size() > 5 ? 5 : big5BuyOverPer3m_sortList.size());
            for (Map<String, Object> map : big5BuyOverPer3m_Top5List) {
                String big5BuyOverPer3m_taxNo = (String) map.get("buyid");
                big5BuyOverPer3mTop5TaxNoList.add(big5BuyOverPer3m_taxNo);
            }
        }
        if (CollectionUtils.isNotEmpty(big5BuyOverPer12m_sortList)) {
            List<Map<String, Object>> big5BuyOverPer12m_Top5List = big5BuyOverPer12m_sortList.subList(0, big5BuyOverPer12m_sortList.size() > 5 ? 5 : big5BuyOverPer12m_sortList.size());
            for (Map<String, Object> map : big5BuyOverPer12m_Top5List) {
                String big5BuyOverPer12m_taxNo = (String) map.get("buyid");
                big5BuyOverPer12mTop5TaxNoList.add(big5BuyOverPer12m_taxNo);
            }
        }
        if (CollectionUtils.isNotEmpty(big5BuyOverPer12mTop5TaxNoList) && CollectionUtils.isNotEmpty(big5BuyOverPer12mTop5TaxNoList)) {
            big5BuyOverPer12mTop5TaxNoList.retainAll(big5BuyOverPer12mTop5TaxNoList);
            resMap.put("Big5BuyOverPer3_12m", big5BuyOverPer12mTop5TaxNoList.size() / (5 * 100));
        }

        if (CollectionUtils.isNotEmpty(big5BuyOverPer12m_list)) {
            big5BuyOverPer24m_list.addAll(big5BuyOverPer12m_list);
        }
        if (CollectionUtils.isNotEmpty(big5BuyOverPerl13l24m_list)) {
            big5BuyOverPer24m_list.addAll(big5BuyOverPerl13l24m_list);
        }
        List<Map<String, Object>> big5BuyOverPer24m_sortList = null;
        List<String> big5BuyOverPer24mTop5TaxNoList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(big5BuyOverPer24m_list)) {
            big5BuyOverPer24m_sortList = sortByDouble(getListBySameKey(big5BuyOverPer24m_list));
            List<Map<String, Object>> big5BuyOverPer24m_Top5List = big5BuyOverPer24m_sortList.subList(0, big5BuyOverPer24m_sortList.size() > 5 ? 5 : big5BuyOverPer24m_sortList.size());
            for (Map<String, Object> map : big5BuyOverPer24m_Top5List) {
                String big5BuyOverPer24m_taxNo = (String) map.get("buyid");
                big5BuyOverPer24mTop5TaxNoList.add(big5BuyOverPer24m_taxNo);
            }
            big5BuyOverPer24mTop5TaxNoList.retainAll(big5BuyOverPer3mTop5TaxNoList);
            resMap.put("Big5BuyOverPer3_24m", big5BuyOverPer24mTop5TaxNoList.size() / (5 * 100));
        }

        resMap.put("Big10BuyOverPer3_6m", big10BuyOverPer3_6m);
        resMap.put("Big10BuyOverPer3_9m", big10BuyOverPer3_9m);

        List<String> big10BuyOverPer3mTop10TaxNoList = new ArrayList<>();
        List<String> big10BuyOverPer12mTop10TaxNoList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(big5BuyOverPer3m_sortList)) {
            List<Map<String, Object>> big10BuyOverPer3m_Top10List = big5BuyOverPer3m_sortList.subList(0, big5BuyOverPer3m_sortList.size() > 10 ? 10 : big5BuyOverPer3m_sortList.size());
            for (Map<String, Object> map : big10BuyOverPer3m_Top10List) {
                String big10BuyOverPer3m_taxNo = (String) map.get("buyid");
                big10BuyOverPer3mTop10TaxNoList.add(big10BuyOverPer3m_taxNo);
            }
        }
        if (CollectionUtils.isNotEmpty(big5BuyOverPer12m_sortList)) {
            List<Map<String, Object>> big10BuyOverPer12m_Top10List = big5BuyOverPer12m_sortList.subList(0, big5BuyOverPer12m_sortList.size() > 10 ? 10 : big5BuyOverPer12m_sortList.size());
            for (Map<String, Object> map : big10BuyOverPer12m_Top10List) {
                String big10BuyOverPer12m_taxNo = (String) map.get("buyid");
                big10BuyOverPer12mTop10TaxNoList.add(big10BuyOverPer12m_taxNo);
            }
        }
        if (CollectionUtils.isNotEmpty(big10BuyOverPer12mTop10TaxNoList) && CollectionUtils.isNotEmpty(big10BuyOverPer3mTop10TaxNoList)) {
            big10BuyOverPer12mTop10TaxNoList.retainAll(big10BuyOverPer3mTop10TaxNoList);
        }
        resMap.put("Big10BuyOverPer3_12m", big10BuyOverPer12mTop10TaxNoList.size() / (10 * 100));

        List<String> big10BuyOverPer24mTop10TaxNoList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(big5BuyOverPer24m_sortList)) {
            List<Map<String, Object>> big5BuyOverPer24m_Top10List = big5BuyOverPer24m_sortList.subList(0, big5BuyOverPer24m_sortList.size() > 10 ? 10 : big5BuyOverPer24m_sortList.size());
            for (Map<String, Object> map : big5BuyOverPer24m_Top10List) {
                String big10BuyOverPer24m_taxNo = (String) map.get("buyid");
                big10BuyOverPer24mTop10TaxNoList.add(big10BuyOverPer24m_taxNo);
            }
        }
        if (CollectionUtils.isNotEmpty(big10BuyOverPer24mTop10TaxNoList) && CollectionUtils.isNotEmpty(big10BuyOverPer3mTop10TaxNoList)) {
            big10BuyOverPer24mTop10TaxNoList.retainAll(big10BuyOverPer3mTop10TaxNoList);
        }
        resMap.put("Big10BuyOverPer3_24m", big10BuyOverPer24mTop10TaxNoList.size() / (10 * 100));

        resMap.put("ZuanPiaoSum24m", sortByStr(zuanPiaoSum24m_list));
        resMap.put("ZuanPiaoTovoidSum24m", sortByStr(zuanPiaoTovoidSum24m_list));
        resMap.put("ZuanPiaoHongChongSum24m", sortByStr(zuanPiaoHongChongSum24m_list));
        resMap.put("PuPiaoSum24m", sortByStr(puPiaoSum24m_list));
        resMap.put("PuPiaoTovoidSum24m", sortByStr(puPiaoTovoidSum24m_list));
        resMap.put("PuPiaoHongChongSum24m", sortByStr(puPiaoHongChongSum24m_list));
        resMap.put("OtherPiaoSum24m", sortByStr(otherPiaoSum24m_list));
        resMap.put("OtherPiaoTovoidSum24m", sortByStr(otherPiaoTovoidSum24m_list));
        resMap.put("OtherPiaoHongChongSum24m", sortByStr(otherPiaoHongChongSum24m_list));

        Map<String, Object> incomZuanPiaoSum24m_map = new HashMap<>();
        incomZuanPiaoSum24m_list.add(incomZuanPiaoSum24m_map);
        resMap.put("IncomZuanPiaoSum24m", incomZuanPiaoSum24m_list);

        Map<String, Object> incomZuanPiaoTovoidSum24m_map = new HashMap<>();
        incomZuanPiaoTovoidSum24m_list.add(incomZuanPiaoTovoidSum24m_map);
        resMap.put("IncomZuanPiaoTovoidSum24m", incomZuanPiaoTovoidSum24m_list);

        Map<String, Object> incomZuanPiaoHongChongSum24m_map = new HashMap<>();
        incomZuanPiaoHongChongSum24m_list.add(incomZuanPiaoHongChongSum24m_map);
        resMap.put("IncomZuanPiaoHongChongSum24m", incomZuanPiaoHongChongSum24m_list);

        Map<String, Object> incomPuPiaoSum24m_map = new HashMap<>();
        incomPuPiaoSum24m_list.add(incomPuPiaoSum24m_map);
        resMap.put("IncomPuPiaoSum24m", incomPuPiaoSum24m_list);

        Map<String, Object> incomPuPiaoTovoidSum24m_map = new HashMap<>();
        incomPuPiaoTovoidSum24m_list.add(incomPuPiaoTovoidSum24m_map);
        resMap.put("IncomPuPiaoTovoidSum24m", incomPuPiaoTovoidSum24m_list);

        Map<String, Object> incomPuPiaoHongChongSum24m_map = new HashMap<>();
        incomPuPiaoHongChongSum24m_list.add(incomPuPiaoHongChongSum24m_map);
        resMap.put("IncomPuPiaoHongChongSum24m", incomPuPiaoHongChongSum24m_list);

        Map<String, Object> incomOtherPiaoSum24m_map = new HashMap<>();
        incomOtherPiaoSum24m_list.add(incomOtherPiaoSum24m_map);
        resMap.put("IncomOtherPiaoSum24m", incomOtherPiaoSum24m_list);

        Map<String, Object> incomOtherPiaoTovoidSum24m_map = new HashMap<>();
        incomOtherPiaoTovoidSum24m_list.add(incomOtherPiaoTovoidSum24m_map);
        resMap.put("IncomOtherPiaoTovoidSum24m", incomOtherPiaoTovoidSum24m_list);

        Map<String, Object> incomOtherPiaoHongChongSum24m_map = new HashMap<>();
        incomOtherPiaoHongChongSum24m_list.add(incomOtherPiaoHongChongSum24m_map);
        resMap.put("IncomOtherPiaoHongChongSum24m", incomOtherPiaoHongChongSum24m_list);

        Map<String, Object> big5BuyidNormalTovoid24m_map = new HashMap<>();
        big5BuyidNormalTovoid24m_list.add(big5BuyidNormalTovoid24m_map);
        resMap.put("Big5BuyidNormalTovoid24m", big5BuyidNormalTovoid24m_list);

        Map<String, Object> big5BuyidHongChong24m_map = new HashMap<>();
        big5BuyidHongChong24m_list.add(big5BuyidHongChong24m_map);
        resMap.put("Big5BuyidHongChong24m", big5BuyidHongChong24m_list);

        if (CollectionUtils.isNotEmpty(big5BuyidNormall12m_list)) {
            big5BuyidNormal24m_list.addAll(big5BuyidNormall12m_list);
        }
        if (CollectionUtils.isNotEmpty(big5BuyidNormall13l24m_list)) {
            big5BuyidNormal24m_list.addAll(big5BuyidNormall13l24m_list);
        }
        if (CollectionUtils.isNotEmpty(big5BuyidNormal24m_list)) {
            List<Map<String, Object>> big5BuyidNormal24m_sortList = sortByDouble(getListBySameKey(big5BuyidNormal24m_list));
            List<Map<String, Object>> big5BuyidNormal24m_Top5List = big5BuyidNormal24m_sortList.subList(0, big5BuyidNormal24m_sortList.size() > 5 ? 5 : big5BuyOverPer24m_sortList.size());
            resMap.put("Big5BuyidNormal24m", big5BuyidNormal24m_Top5List);
        }

        resMap.put("endtime", String.valueOf(System.currentTimeMillis() / 1000));
        return resMap;
    }


    private List<Map<String, Object>> getListBySameKey(List<Map<String, Object>> list) {
        List<Map<String, Object>> newMapList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            String oldBuyid = (String) list.get(i).get("buyid");
            boolean flag = false;
            for (int j = 0; j < newMapList.size(); j++) {
                String newBuyid = (String) newMapList.get(j).get("buyid");
                if (newBuyid.equals(oldBuyid)) {
                    Double total = Double.valueOf(list.get(i).get("total").toString()) + Double.valueOf(list.get(j).get("total").toString());
                    newMapList.get(j).put("total", total);
                    flag = true;
                }
            }
            if (flag == false) {
                newMapList.add(list.get(i));
            }
        }
        return newMapList;
    }

    private List<Map<String, Object>> sortByDouble(List<Map<String, Object>> list) {
        if (!CollectionUtils.isNotEmpty(list)) {
            return null;
        }
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            //实现一个比较器
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Double d1 = (Double) o1.get("total");
                Double d2 = (Double) o2.get("total");
                return (d2).compareTo(d1);//降序
            }
        });
        return list;
    }

    // 根据日期倒序排序
    private List<Map<String, Object>> sortByStr(List<Map<String, Object>> list) {
        if (!CollectionUtils.isNotEmpty(list)) {
            return null;
        }
        Collections.sort(list, new Comparator<Map<String, Object>>() {
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                String s1 = (String) o1.get("month");
                String s2 = (String) o2.get("month");
                return s2.compareTo(s1);
            }
        });
        return list;
    }

    /**
     * 给定时间区内的所有月份
     *
     * @param startTime
     * @param endTime
     * @return
     */
    public List<String> getMonthsBetweenDates(String startTime, String endTime) {
        List<String> result = new ArrayList<>();
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
            // 定义起始日期
            Date d1 = new SimpleDateFormat("yyyy-MM").parse(startTime);
            // 定义结束日期 可以去当前月也可以手动写日期。
            Date d2 = new SimpleDateFormat("yyyy-MM").parse(endTime);
            // 定义日期实例
            Calendar dd = Calendar.getInstance();
            // 设置日期起始时间
            dd.setTime(d1);
            result.add(format.format(d1));
            // 判断是否到结束日期
            while (dd.getTime().before(d2)) {
                // 进行当前日期月份加1
                dd.add(Calendar.MONTH, 1);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                String str = sdf.format(dd.getTime());
                result.add(str);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @GetMapping("/exception")
    public String exceptionTest() {
        String text = "1";
        try {
            int i = 10/2;
        } catch (Exception e) {
            text = text + "2";
        }
        text = text + "3";
        return text;
    }

    @GetMapping("/color")
    public String testColor() {
//        return getFormatLogString("重试超次", 31, 0);
        return "风控规则异常：" + "<font color=#FF0000>" + "重试超次" + "</font>" + "\n" + "<font color='blue'>" + "已处理" + "</font>";
    }

    private static String getFormatLogString(String content, int colour, int type) {
        boolean hasType = type != 1 && type != 3 && type != 4;
        if (hasType) {
            return String.format("\033[%dm%s\033[0m", colour, content);
        } else {
            return String.format("\033[%d;%dm%s\033[0m", colour, type, content);
        }
    }

    // 4、修改用户
    @PostMapping("/updateUser")
    public R updateUser(@RequestBody User user) {
        int i = userService.updateUser(user);
        if (i == 1) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    // 3、根据id删除用户
    @GetMapping("/deleteUser/{id}")
    public R deleteUser(@PathVariable Integer id) {
        int i = userService.deleteUser(id);
        if (i == 1) {
            return R.ok().message("删除成功");
        }
        return R.error().message("删除失败");
    }

    // 2、新增用户
    @PostMapping("/saveUser")
    public R saveUser(@RequestBody User user) {
        int i = userService.saveUser(user);
        if (i == 1) {
            return R.ok().message("新增成功");
        }
        return R.error().message("新增失败");
    }

    // 1、查询所有用户
    @GetMapping("/queryAllUser")
    public R queryAllUser() {
        List<User> list = userService.queryAllUser();
        return R.ok().data("items",list);
    }

}
